import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import pygetwindow
import pyautogui

from Moniter.MainMoniter import Moniter


class MonController:
    def __init__(self):
        self.m = Moniter()

    def check(self):
        print('comming into check')
        return self._do_check() and self.m.check()

    def get_stock(self):
        new2, stock2 = self.m.main_new
        return stock2

    # def run(self):
    #     while True:
    #         if not self.start():  # 启动失败
    #             time.sleep(1)
    #         else:                   # 启动成功
    #             while True:         # 开始之后的主循环
    #                 time.sleep(0.2)
    #                 is_new, stock = self.get_new()
    #                 if is_new:
    #                     print('**MoniterController**', stock)

    def _do_check(self):
        # TODO:但凡弹窗就卡，艹；可以用QDiolog,书P191
        wins = pygetwindow.getWindowsWithTitle('网上股票交易系统5.')
        # wins = pygetwindow.getWindowsWithTitle('记事本')
        print('get 交易', wins)

        if len(wins) != 1:
            text = '未打开东海证券网上交易系统或打开了其他软件，请关闭命名相似的软件，再重试'
            # dialog = QWidget()# 这里不太行
            # reply = QMessageBox.critical(QWidget, '警告', text, QMessageBox.Yes | QMessageBox.No, QMessageBox.YES)
            # print(reply)
            # dialog = QDialog()
            # btn = QPushButton('ok', dialog)
            # btn.move(50, 50)
            # dialog.setWindowTitle('aaaa')
            # dialog.setWindowModality(Qt.Application)
            # dialog.exec_()
            # pyautogui.alert(title='警告', text='未打开东海证券网上交易系统或打开了其他软件，请关闭命名相似的软件，再重试', button='我再试试看')
            return False
        else:
            return True
